$(function(){
	var i = 0;
	//发送消息功能
	function sendtext(){
		var $text = $(".text").val();
		var $content = "<br><div><p>"+$text+"</p><span></span><img src='img/myavatar.jpg'></div><br><br>";
		console.log($text);

		//获取时间
		var mydate = new Date();

		if($text == ""){
			alert("内容为空请重新输入");
			$(".text").val("");
		}else{
			$(".active .chat_right").append($content);
			$(".text").val("");
			var $textHeight = $(".active .chat_right div").height();
			var $num = $(".active .chat_right").children().length;
			console.log($num);
			console.log($textHeight);
			var $allHeight = ($textHeight+30) * $num;
			console.log($allHeight);
			i++;
			$(".chatroom.active").scrollTop(i*600);
			$(".diaNow .last_text").html($text);

			var hour = mydate.getHours();
			var minute = mydate.getMinutes();
			var time = hour + ":" + minute;
			$(".diaNow .time").html(time);
		}
	}



	$(".text_submit").click(function(){
		sendtext();
	});


	$("textarea").keydown(function(e){
	    if (e.keyCode == "13") {
		 	sendtext();
		 	e.preventDefault();
	    }
	});


	//消息上浮

	//切换聊天窗口

	function addchat(e){
		$num = e.index();
		$(".dia").removeClass("diaNow");
		e.addClass("diaNow");
		var $nums = $num;

		var chatContent = "<div class='chatroom chat"+$num+"'><div class='rel'><div class='chat_right'></div></div></div>"
		
		if(!$(".chatroom").hasClass("chat"+$num)){
			$(".edit").before(chatContent);
			// alert("1");
		}


		$nowChat = $(".chatroom").eq($num);
		$(".chatroom").removeClass("active");
		$(".chat"+$num).addClass("active");
	}

	$(".btn_msg").click(function(){
		alert("1");
		$(".dia").removeClass("newChat");
		$("#friend").css({
			"display":"none"
		});
		var dialogList = "<li class='dia newChat'><img class='avatar' src='img/avatar1.png'><p class='name'>一波</p><p class='last_text'></p><p class='time'></p></li>"

		$("#chat .dialog_list").append(dialogList);


		addchat($(".newChat"));
	});


	//切换聊天室
	$("body").on("click",".dia",function(){      //解决动态元素无法被选择器选中的问题，jquery 1.7+之后用on代替live,on()方法在被选元素及子元素上添加一个或多个事件处理程序
		addchat($(this));
	})


	//切换联系人窗口
	$(".contact_list a").click(function(e){
		$("#friend").css({
			"display":"block"
		});
		e.preventDefault();
	});

	//切换聊天窗口
	$(".dialog a").click(function(e){
		$("#friend").css({
			"display":"none"
		});
		e.preventDefault();
	});

	//切换个人简介

	var $data = data;
	var html = $(".introsec").html();

	function addIntro(n){

		var template = $(".introsec").html();
		// console.log(template);

		// for (s in data){
			var _html = template
								.replace('{{name}}',$data[n].name)
								.replace('{{signature}}',$data[n].signature)
								.replace('{{remark}}',$data[n].remark)
								.replace('{{region}}',$data[n].region)
								.replace('{{id}}',$data[n].id);
			$(".introsec").html(_html);

		// }

	}

	$("#friend .dia").click(function(){
		// alert(1);
		// console.log(html);
		// console.log(1);
		$(".introsec").html(html);
		var n = $(this).index();
		console.log(n);
		addIntro(n);
		$(".introsec").css({
			"display":"block"
		});
		$(".btn_msg").css({
			"display":"block"
		});
	});






});


